
/*
	Button Functions
	Updated: always updating
	
	Author: Jared Bayne
	E-Mail:		JaredNBayne@gmail.com
	Created On:	February 13th, 2008

	Menu:
	-------
	Created 4/16/09 1:40am
	E-Mail: contactus@wolfcrown.com
	Author: Dustin McBEth

	Purpose:	This file will handle all of the button functions
	



*/

#define BTop		GET_BIT(PINA).bit0
#define BLeft		GET_BIT(PINA).bit1
#define BMiddle		GET_BIT(PINA).bit2
#define BRight		GET_BIT(PINA).bit3
#define BBottom		GET_BIT(PINA).bit4

#ifndef OFF
#define OFF 0
#endif

#ifndef ON
#define ON 1
#endif



// Dustin's RE-Defines
#define BUTTON_TOP		0
#define BUTTON_LEFT		1
#define BUTTON_CENTER 	2
#define BUTTON_RIGHT	3
#define BUTTON_BOTTOM	4
/**********************************************
*
*		Please do not use these.
*
*		To whomever wrote this, please see the chapter in K&R that describes the
*		C preprocessor directive "#define".  Then take a look in the cpu2comms.h
*		file in the CPU2_SetAddress(int) function that uses local variables by
*		the same name.  This causes a compile error, because they have been
*		defined here.
*
*		Thanks.
*
*		- Tim Haines 04/04/2009 08:38PM
*
//pickup order definitions
//#define	one 	1
//#define	two 	2
//#define 	three 	3
//#define 	four 	4
**********************************************/

int pickup_one = 1;
int pickup_two = 2;
int pickup_three = 3;
int pickup_four = 4;

#define yes	1	// is defined in init file
#define no 0   // is defined in init file


void Line_Up_Bot(void);
void Input_Sequence(void);
int Read_Button_Press(void);
void simulate_input_sequence(void);
int Wait_For_Wireless_Start(void);
void drive_with_buttons(int speed);
int Scan_Button_Press(void);
void wait_for_button_press(void);

void wait_for_button_press()
{
	while (Scan_Button_Press()==0);

}


void drive_with_buttons(int speed)
{
	
	
	switch (Scan_Button_Press())
	{
		case 1  : 
			goleft(speed);
			break;
		case 2  : 
			stop();
			break;
		case 3  : 
			goright(speed);
			break;
		case 4  : 
			goforward(speed);
			break;
		case 5  :
			gobackward(speed);
			break;
		default: 
			stop();								
			break;
	}


}




void simulate_input_sequence(void)
{

	pickup_one=1;
	pickup_two=2;
	pickup_three=3;
	pickup_four=4;

}




// Menu
// --------
// Three steps
// 1.) We ask if we want to pick corners
//  - Left button yes as in : Yes (L)
//  - Right button no as in : No (R) 
// If yes, we do the button thing
//
// The Button thing:
// 1.a.) State what buttons are what number
//  Text to LCD: "Top 1 : R 2
//  Text to LCD: "BTM 4 : L 3
//
// 1.b) get input
//	- Confirm room
//  - if not correct, repeat
//  - repeat until all rooms
//
// if no, Step 2, we skip to calibration
// 
// 2.) Calibrate 
//
// 3.) Press button to go


void Input_Sequence(void)
{
	// btn = Button, finalbtn = Final Button	
	writeln("Received");
	_delay_ms(750);
	
// 1.) We ask if we want to pick corners
//  - Left button yes as in : Yes (L)
//  - Right button no as in : No (R) 
// If yes, we do the button thing
// if no, Step 2, we skip to calibration
//           .........1......
	int choice=0, done = 0;
	do{
	  
	  writeln("Input Corners?");
	  lcdline2();
	  write("Yes (L)   No (R)");
	  _delay_ms(250);
	  // get data
	  choice = Read_Button_Press();
	  
	  // If yes, we do the button thing
	  if(choice == BUTTON_RIGHT) {
		done = 1;
		// Noted out is for bug testing
//		writeln("  ");
//		lcddispint(choice);
//		_delay_ms(1000);
	      return; // other solution would take too much coding
		  // bad form I know
		}
		
	  if(choice == BUTTON_LEFT) { 
		done = 1;
		// Noted out is for bug testing
//		writeln("  ");
//		lcddispint(choice);
//		_delay_ms(1000);
			
		}
		
	  if(done==0) {
	  // if you hit any other button!
	    writeln("Try again!");
		// Noted out is for bug testing
//		lcdline2();
//		lcddispint(choice);
//		_delay_ms(1000);
	    
		_delay_ms(1000);
	  }
	} while(done==0);

    done = 0;
// The Button thing:
// 1.a.) State what buttons are what number
//  Text to LCD: "Top 1 : L 2"
//  Text to LCD: "BTM 4 : R 3"
//  state corner numbers

	writeln("Top 1 : L 2");
	lcdline2();
	write("BTM 4 : R 3");
	_delay_ms(2000);
	
// 1.b) get input
//	- Confirm room
//  - if not correct, repeat
//  - repeat until all rooms

	choice = 0; // input from display
	// Set it so we can't select the same corner
	
	// States what room we are working on
	int roomCountTemp = 1;

	// use global int array: roomOrderToGoTo[4];
	// as your variable to store room number;
	
	// Get room number input
 	do {
// 1.b) get input
//	- Confirm room
//  - if not correct, repeat
//  - repeat until all rooms
	  writeln("Room ");
	  lcddispint(roomCountTemp); // write and int to LCD
	  write(": ");
	  lcdline2();				// go to second line
	  
	  // we are displaying rooms available
	  for(int i=0; i<4; i++) {
	    if(isRoomSelectedAlready[i]==0) {
	      lcddispint(i+1);
		  lcddispchar(' ');
		}
	  }
	  
	  choice = Read_Button_Press();
	  
	  // buttons are top 4, left 1, right 3, bottom 5
	  // resetting button choice numbers to correct room numbers: top 1, left 2, right 3, bottom 4
	  
	  switch(choice) {
	    case 4: { 
			choice = 1; // top
			break;
			}
		
		case 1: { 
			choice = 2; // left
			break;
		}
	  
		case 3: { // this is correct, do nothing
						// right
			break;
		}
		case 5: { 
			choice = 4; // bottom
			break;
		}
		default: { 
			choice = 0; // top
			//       .........1......
			writeln("room 5 Try again");
			_delay_ms(750);
			break;
		}
	  }	// close of switch statement
	  
	  
		// Test if room has been selected already
		// loop through all four rooms
		// if we are at five, we are too high, 4 max
		for(int i=1; i<5; i++) {
		  if(choice == i) {
			if(isRoomSelectedAlready[i-1]==0) {
			  // set roomOrderToGoTo[i] to choice
			  // set isRoomSelectedAlready ==1
			  isRoomSelectedAlready[i-1] = 1;
			
			  // roomCountTemp is just the room we are getting input for.
			  // for arrays you have to -1 to get the real place in the array
			  // roomCountTemp is set to 1 to start with
			  roomOrderToGoTo[roomCountTemp-1] = choice;
			
			  // display room acceptable
			  writeln("Choice ");
			  lcddispint(choice);
			  lcdline2();
			  write("Accepted");
			  _delay_ms(750);
			
			  if(roomCountTemp<4) {
			    roomCountTemp++;
		      } else {
			    done=1;
			  }
			  // else done
		    } else {
		      // Room is already selected
		      //	   .........1......
		      writeln("Room used!");
		      _delay_ms(750);
		    } // close if loop
		   } // close if which tested choice
		  } // close for loop
	} while(done==0);
	
		//If we make it here, we have confirmed the sequence
		//So lets actually set it
	
	writeln("Input Complete");
	_delay_ms(750);

// 2.) Calibrate 
//
// 3.) Press button to go	
}


int Read_Button_Press(void)
{
	int pressed=0;
	
	while (pressed==0)
	{
		if (BLeft==OFF)
			pressed=1;
		else if (BMiddle==OFF)
			pressed=2;
		else if (BRight==OFF)
			pressed=3;
		else if (BTop==OFF)
			pressed=4;
		else if (BBottom==OFF)
			pressed=5;
	
	
	
	}


	return pressed;
}

int Scan_Button_Press(void)
{
	int pressed=0;

	if (BLeft==OFF)
		pressed=1;
	else if (BMiddle==OFF)
		pressed=2;
	else if (BRight==OFF)
		pressed=3;
	else if (BTop==OFF)
		pressed=5;
	else if (BBottom==OFF)
		pressed=4;

	return pressed;
}

